package com.woniuxy.controller;

import com.woniuxy.pojo.Menu;
import com.woniuxy.service.RoleService;
import com.woniuxy.util.DataGridView;
import com.woniuxy.util.ResultDto;
import com.woniuxy.vo.RoleVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class RoleController {
    @Autowired
    private RoleService roleService;

    @GetMapping("/roles")
    public DataGridView selectRolesByPage(RoleVo roleVo) {
        return roleService.selectRolesByPage(roleVo);
    }

    @GetMapping("/roles/selectAllMenus.do")
    public List<Menu> selectAllMenus() {
        return this.roleService.selectAllMenus();
    }

    @GetMapping(value = "/roles/getChecked.do")
    public String[] getChecked(int rid) {
        return this.roleService.selectMenuIdsByRid(rid);
    }

    @GetMapping(value = "/roles/assignRight.do")
    public ResultDto assignRight(int rid, @RequestParam("mids[]") String[] mids) {
        try {
            this.roleService.assignRight(rid, mids);
            return ResultDto.RIGHT_GRANT_SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return ResultDto.RIGHT_GRANT_FAILURE;
        }
    }

    @DeleteMapping(value = "/roles/{id}")
    public ResultDto deleteRole(@PathVariable("id") int id) {
        try {
            this.roleService.removeRoleById(id);
            return ResultDto.ROLE_DELETE_SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return ResultDto.ROLE_DELETE_FAILURE;
        }
    }
}
